package com.esunny.online.controller;

import com.esunny.online.domain.dto.ReturnStructure;
import com.esunny.online.domain.entity.Tag;
import com.esunny.online.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Bcc
 * @Date: 2020/8/7 10:54
 * @Description:
 * @modified By:
 * @version: $
 */

@Api(value = "标签请求控制器")
@Controller
@RequestMapping("/tag")
public class TagController {

    @Resource
    private TagService tagService;

    @Value("${server.servlet.context-path}")
    private String contextPath;

    private Logger logger = LoggerFactory.getLogger(TagController.class);

    /**
     * 获取资源标签列表
     * @return 标签列表
     */
    @ApiOperation(value = "获取资源标签列表", notes = "获取全部标签")
    @ApiResponse(code = 400, message = "请求失败", response = ReturnStructure.class)
    @GetMapping("/getTagList")
    @ResponseBody
    public ReturnStructure getTagList(){
        Map<String,Object> map = new HashMap<>(0);

        List<Tag> tagList = tagService.getTagList();
        String message;
        int code;
        if (tagList != null){
            map.put("tagList",tagList);
            message = "获取标签列表成功";
            code = 200;
        }else{
            map.put("tagList",null);
            message = "标签列表无数据或获取失败";
            code = 404;
        }

        return new ReturnStructure(code,message,map);
    }

    /**
     * 添加资源标签
     * @return 返回体
     */
    @ApiOperation(value = "添加资源标签", notes = "添加标签")
    @ApiResponse(code = 400, message = "请求失败", response = ReturnStructure.class)
    @PostMapping("/addTag")
    @ResponseBody
    public ReturnStructure addTag(@RequestBody Tag tag){
        Map<String,Object> map = new HashMap<>(0);

        String message;
        int code;
        if (tag != null){
            tagService.addTag(tag);
            message = "添加标签成功";
            code = 200;
        }else{
            message = "无参数";
            code = 404;
        }
        return new ReturnStructure(code,message,map);
    }

    /**
     * 彻底删除资源标签
     * @return 返回体
     */
    @ApiOperation(value = "删除资源标签", notes = "删除标签")
    @ApiResponse(code = 400, message = "请求失败", response = ReturnStructure.class)
    @PostMapping("/deleteTag")
    @ResponseBody
    public ReturnStructure deleteTag(@RequestBody Tag tag){
        Map<String,Object> map = new HashMap<>(0);

        String message;
        int code;
        if (tag != null){
            tagService.deleteTag(tag);
            message = "彻底删除标签成功";
            code = 200;
        }else{
            message = "无参数";
            code = 404;
        }
        return new ReturnStructure(code,message,map);
    }

    /**
     * 更改资源标签状态
     * @return 返回体
     */
    @ApiOperation(value = "更改资源标签状态", notes = "更新标签")
    @ApiResponse(code = 400, message = "请求失败", response = ReturnStructure.class)
    @PostMapping("/updateTagStatus")
    @ResponseBody
    public ReturnStructure updateTagStatus(@RequestBody Tag tag){
        Map<String,Object> map = new HashMap<>(0);

        String message;
        int code;
        if (tag != null){
            tagService.updateTagStatus(tag);
            message = "更改资源标签状态成功";
            code = 200;
        }else{
            message = "无参数";
            code = 404;
        }
        return new ReturnStructure(code,message,map);
    }

}
